<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\reimbursement;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\reimbursement\ExpenseApplication;
use app\common\lists\ListsSearchInterface;


/**
 * ExpenseApplication列表
 * Class ExpenseApplicationLists
 * @package app\adminapi\listsreimbursement
 */
class ExpenseApplicationLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function setSearch(): array
    {
        return [
            '=' => ['current_status', 'applicant_id', 'expense_type_id'],
            '%like%' => ['description'],
            'between' => ['amount'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function lists(): array
    {
        $list = ExpenseApplication::with(['applicant', 'expenseType'])
            ->where($this->searchWhere)
            ->field(['id', 'applicant_id', 'expense_type_id', 'amount', 'description', 'current_status'])
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $this->page_no ?? 1,
                'list_rows' => $this->page_size ?? 15
            ])
            ->toArray();

        $statusMap = [
            0 => '待提交',
            1 => '待审核',
            2 => '已退回',
            3 => '已通过',
        ];

        // 直接返回处理后的列表数据
        return array_map(function ($item) use ($statusMap) {
            $item['applicant_name'] = $item['applicant']['nickname'] ?? '';
            $item['expense_type_name'] = $item['expense_type']['name'] ?? '';
            unset($item['applicant'], $item['expense_type']);
            $item['status_desc'] = $statusMap[$item['current_status']] ?? '未知状态';
            $item['auto_approved'] = ($item['current_status'] == 3) ? '是' : '否';
            return $item;
        }, $list['data']);
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function count(): int
    {
        return ExpenseApplication::where($this->searchWhere)->count();
    }

}